using UnityEngine;
using System.Collections;

public class Life : MonoBehaviour {
	
	Transform thisTransform;
	Vector3 maxScale;
	Vector3 lerpScale;
	Vector3 lerpPos;
	
	void Awake()
	{
		GameController.OnPainLevelChange += painLevelChange;
	}
	
	void OnDestroy()
	{
		GameController.OnPainLevelChange -= painLevelChange;
	}
	
	// Use this for initialization
	void Start () {
		thisTransform = transform;
		maxScale = thisTransform.localScale;
		StartCoroutine("reflectLevel");
	}
	
	void painLevelChange(float painLevel)
	{
		//StopCoroutine("reflectLevel");
		//StartCoroutine("reflectLevel", painLevel);
	}
	
	IEnumerator reflectLevel()
	{
		Vector3 origPos = thisTransform.position;
		lerpPos = thisTransform.position;
		//float time = .2f, refTime = 0;
		float oldScale = thisTransform.localScale.y;
		lerpScale = thisTransform.localScale;
		float maxLevel = GameController.instance.maxPainLevel;
		//float targetScale = level * maxScale.y / maxLevel;
		//print(targetScale + " " + targetScale);
		while(true)
		{
			
			float targetScale = (maxLevel - GameController.instance.PainLevel) * maxScale.y / maxLevel;
			lerpScale.y = targetScale;//Mathf.MoveTowards(oldScale, targetScale, 50 * Time.deltaTime);
			
			lerpPos.y = origPos.y + targetScale/2 ;
			thisTransform.position = lerpPos;
			
			//print(lerpScale.y);
			thisTransform.localScale = lerpScale;
			yield return 0;
			//refTime += Time.deltaTime;
		}
	}
}
